//
//  HJChatRightMsgCell.m
//  project
//
//  Created by hu on 2019/3/27.
//  Copyright © 2019 健健. All rights reserved.
//

#import "HJChatRightMsgCell.h"
@interface HJChatRightMsgCell ()
/*
 * 头像父视图
 */
@property (weak, nonatomic) IBOutlet UIView *headSubView;
/*
 * 头像图片
 */
@property (weak, nonatomic) IBOutlet UIImageView *headImage;
/*
 * 消息内容
 */
@property (weak, nonatomic) IBOutlet UILabel *contentMsg;


@property (weak, nonatomic) IBOutlet UIImageView *rightBGImageView;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *contentMsg_Width;
@property (nonatomic,strong) RCTextMessage * textMessage;

@end

@implementation HJChatRightMsgCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // 用户图片 圆角
    [self.headImage rounded:(UIRectCornerAllCorners) radius:20];
    // 图片外的圆环
    [self.headSubView rounded:(UIRectCornerAllCorners) radius:20 borderColor:[UIColor blackColor] borderWidth:1];
    self.sendError.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [self.rightBGImageView rounded:(UIRectCornerTopLeft | UIRectCornerBottomLeft | UIRectCornerBottomRight) radius:12];
    
    
}

// 发送失败
- (IBAction)sendErrorBtnClicked:(UIButton *)sender {
    WS(weakSelf)
    UIAlertController * alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction * okAlert = [UIAlertAction actionWithTitle:@"重新发送" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [[RCIMClient sharedRCIMClient] sendMessage:ConversationType_PRIVATE targetId:weakSelf.targetId content:weakSelf.textMessage pushContent:weakSelf.textMessage.content pushData:nil success:^(long messageId) {
//            NSLog(@"发送成功。当前消息ID：%ld", messageId);
            dispatch_async(dispatch_get_main_queue(), ^{
                weakSelf.sendError.hidden = YES;
            });
        } error:^(RCErrorCode nErrorCode, long messageId) {
//            NSLog(@"发送失败。消息ID：%ld， 错误码：%ld", messageId, (long)nErrorCode);
            dispatch_async(dispatch_get_main_queue(), ^{
                weakSelf.sendError.hidden = NO;
            });
        }];
    }];
    UIAlertAction * cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alert addAction:okAlert];
    [alert addAction:cancel];
    [[UIViewController currentViewController] presentViewController:alert animated:YES completion:nil];
}

-(void)loadRightMoreWithData:(RCTextMessage *)data {
    HJUserModel * userModel = [HJUserManager getUserModel];
    self.textMessage = data;
    self.textMessage.senderUserInfo = [[RCUserInfo alloc]initWithUserId:userModel.userId name:userModel.nickName portrait:userModel.avatar];
    self.contentMsg.text = data.content;
    [self.headImage sd_setImageWithURL:[NSURL URLWithString:userModel.avatar] placeholderImage:setImage(@"img_mydongtai_defualt_avatar")];
    CGFloat totalWidth = SCREEN_WIDTH - 140;
    
    CGSize size = [CommonTools sizeOfText:data.content fontSize:14 width:totalWidth];
//    NSLog(@"right==%@",NSStringFromCGSize(size));
    CGFloat allWidth = size.width+10;
    if (allWidth<=totalWidth) {
        self.contentMsg.textAlignment = NSTextAlignmentLeft;
        self.contentMsg_Width.constant = allWidth;
    }else {
        self.contentMsg.textAlignment = NSTextAlignmentRight;
        self.contentMsg_Width.constant = totalWidth;
    }
}
@end
